package com.itheima.web.client;

import com.itheima.dao.UserMapper;
import com.itheima.model.domain.User;
import com.itheima.model.domain.User_Authority;
import com.itheima.service.IArticleService;
import com.itheima.service.ICommentService;
import com.itheima.service.IUser_AuthorityService;
import com.itheima.service.IuserService;
import com.itheima.service.impl.UserServiceImpl;
//import com.itheima.service.IuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
/**
 * @Classname LoginController
 * @Description 用户登录模块
 * @Date 2019-3-14 14:15
 * @Created by CrazyStone
 */
@Controller
public class LoginController {

    @Autowired
    private ICommentService commentServcieImpl;

    @Autowired
    private IuserService iuserService;

    @Autowired
    private IUser_AuthorityService iUser_authorityService;

    @Autowired
    private IuserService  userServiceImpl;

    // 向登录页面跳转，同时封装原始页面地址
    @GetMapping(value = "/login")
    public String login(HttpServletRequest request, Map map) {
        // 分别获取请求头和参数url中的原始访问路径
        String referer = request.getHeader("Referer");
        String url = request.getParameter("url");
        System.out.println("referer= "+referer);
        System.out.println("url= "+url);
        //截住来自表单的login
        // 如果参数url中已经封装了原始页面路径，直接返回该路径
        if (url!=null && !url.equals("")){//什么都没填
            map.put("url",url);
            return "back/register";
            // 如果请求头本身包含登录，将重定向url设为空，让后台通过用户角色进行选择跳转
        }else if (referer!=null && referer.contains("/login")){//填了就跳到security config中log
            map.put("url", "");
        }
        else {
            // 否则的话，就记住请求头中的原始访问路径
            map.put("url", referer);
        }
        return "comm/login";
    }

    @GetMapping("/register")
    public String toRegister(){
        return "back/register";
    }

    @PostMapping("/register_s")
    public String register_s(HttpServletRequest request, MultipartFile fileUpload){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        User user = new User();
        MultipartFile file = fileUpload;
//        for (MultipartFile file1 : fileUpload){
        String originalFilename = file.getOriginalFilename();//原来的图片名
        String ext =  originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = username + ext;

        System.out.println(originalFilename);
        System.out.println(ext);
        System.out.println(filename);

        String dirPath = "E:/java/blog_system/src/main/resources/static/assets/img/";
        File filePath = new File(dirPath);
        if(!filePath.exists()){
            filePath.mkdir();
        }
        try{
            file.transferTo(new File(dirPath+filename));
        }catch (Exception e){
            System.out.println("HelloWorld");
        }


        user.setUsername(username);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password2 =  passwordEncoder.encode(password);
        user.setPassword(password2);
        user.setEmail(email);
        user.setImg(dirPath+filename);
        iuserService.publish(user);
        System.out.println("id:"+user.getId());
        User_Authority user_authority = new User_Authority();
        user_authority.setUserid(user.getId());
        iUser_authorityService.publish(user_authority);
//        }



//            String filename = file.getOriginalFilename();
//            filename = user.getId()+"_"+filename;



        return "comm/login";
    }


    // 对Security拦截的无权限访问异常处理路径映射
    @GetMapping(value = "/errorPage/{page}/{code}")
    public String AccessExecptionHandler(@PathVariable("page") String page, @PathVariable("code") String code) {
        return page+"/"+code;
    }
}

